//-----------------------------------------------------------------------------
//クラス名：ClubEditConfirmation.java
//所    属：H3A1 04
//名    前：大瀬碩
//説    明：編集したクラブ情報をDBに保存する
//-----------------------------------------------------------------------------
package club;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Calendar;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


//@WebServlet("/ClubEditConfirmation")
public class ClubEditConfirmation extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

    public ClubEditConfirmation() {
        super();
       
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession(true);
		
		Object doa = session.getAttribute("doa");
		Object poa = session.getAttribute("poa");
		Object act = session.getAttribute("act");
		Object sel_club = session.getAttribute("sel_club");
		
	 	//DB接続
	 	ServletContext context = this.getServletContext();
		String path = context.getRealPath("/WEB-INF/connect.properties");
		
		InputStream in = new FileInputStream(path);
		Properties properties = new Properties();
		properties.load(in);
		in.close();
		
		Calendar cal1 = Calendar.getInstance();
		int year = cal1.get(Calendar.YEAR);
		int month = cal1.get(Calendar.MONTH) + 1;
		int day = cal1.get(Calendar.DATE);
	 	
	    Connection conn = null;
	    String url = properties.getProperty("key1");
	    String user = properties.getProperty("key2");
	    String password = properties.getProperty("key3");
	    
	    //UPDATAで更新
	    try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
			conn = DriverManager.getConnection(url, user, password);
			
			Statement stmt = conn.createStatement();
		    String sql = "UPDATE club SET activity_time = '"+doa+"', activity_place = '"
					      +poa+"', activities = '"+act+"', updated_date = '"+year+"/"+month+"/"+day+
					      "' WHERE club_id = "+sel_club;
		    
		    int num = stmt.executeUpdate(sql);
	    }catch (ClassNotFoundException e){
	    	System.out.println(e.getMessage());
	    }catch (SQLException e){
	    	
	    	System.out.println(e.getMessage());
	    }catch (Exception e){
	    	
	    }finally{
	      try{
	        if (conn != null){
	          conn.close();
	        }
	      }catch (SQLException e){
	        
	      }
	    }
	    RequestDispatcher rd = request.getRequestDispatcher("Completion(ClubEdit).jsp");
		rd.forward(request,response);
	}
}
